'use strict';

const Controller = require('egg').Controller;

class UserController extends Controller {

    async initUser() {
        const { app, ctx, service } = this;
        const result = await app.model.User.createTable();
        ctx.successResult(result);
    }

    async userRegister() {
        const { ctx, service } = this;
        const { body } = ctx.request;
        const { username, password } = body;

        if (!username) {
            return ctx.errorResult('username missing');
        }
        if (!password) {
            return ctx.errorResult('password missing');
        }

        try {
            const userType = service.constant.getUserType();
            const userData = {
                ...body,
                ...{
                    type: userType.user.value
                }
            };
            const result = await service.user.addUserSrv(userData);
            if (result) {
                const { dataValues } = result;
                const { username, password, type, role_id, ...otherData } = dataValues;
                const authToken = ctx.helper.jwtSign({username, type, roleId: role_id});
                await ctx.helper.setRedisStr(`user:${username}`, authToken);
                ctx.successResult({
                    ...otherData,
                    ...{
                        username,
                        role_id,
                        authToken
                    }
                });
            } else {
                ctx.errorResult('user exist');
            }
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async userLogin() {
        const { ctx, service } = this;
        const { body } = ctx.request;
        const { username, password } = body;

        if (!username) {
            return ctx.errorResult('username missing');
        }
        if (!password) {
            return ctx.errorResult('password missing');
        }

        try {
            const result = await service.admin.getUserSrv(body);
            if (result) {
                const { dataValues } = result;
                const { username, type, role_id } = dataValues;
                const authToken = ctx.helper.jwtSign({username, type, roleId: role_id});
                await ctx.helper.setRedisStr(`user:${username}`, authToken);
                ctx.successResult({
                    ...dataValues,
                    ...{
                        authToken
                    }
                });
            } else {
                ctx.errorResult('no user');
            }
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async addUser() {
        const { app, ctx, service } = this;
        const { body } = ctx.request;
        console.log('addUser body:', body);

        const result = await service.user.addUserSrv(body);
        if (result) {
            ctx.successResult(result);
        } else {
            ctx.errorResult('user exist');
        }
    }

    async editUser() {
        const { app, ctx, service } = this;
        const data = await service.news.getNewsSrv();
        ctx.successResult(data);
    }

    async getUser() {
        const { app, ctx, logger, service } = this;
        const { userId } = ctx.params;

        if (!userId) {
            return ctx.errorResult('userId missing');
        }

        const result = await service.user.getUserByIdSrv(userId);
        ctx.successResult(result);
    }

    async getUserAll() {
        const { app, ctx, logger, service } = this;
        const result = await service.user.getUserAllSrv();
        ctx.successResult(result);
    }

    async delUser() {
        const { app, ctx, logger } = this;
        const { userId } = ctx.params;

        if (!userId) {
            return ctx.errorResult('userId missing');
        }

        const result = await service.user.delUserByIdSrv(userId);
        ctx.successResult(result);
    }
}

module.exports = UserController;
